package com.hd.trans.files.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.hd.trans.R;
import com.hd.trans.db.bean.MediaItem;
import com.hd.trans.files.bean.MimeType;
import com.hd.trans.files.holder.MediaFileViewHolder;
import com.hd.trans.ui.listener.HDOnClickListener;
import com.hd.trans.utils.StringUtils;
import com.hd.trans.widgets.VideoAlbumItem;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Objects;

/**
 * Author: yangtianwen
 * Time: 2021/12/14
 * Des: 选择文件视频列表
 */
public class VideoAlbumAdapter extends RecyclerView.Adapter<MediaFileViewHolder> {

    private MutableLiveData<LinkedHashMap<Long, MediaItem>> mSelectedVideoList;

    private ArrayList<MediaItem> mList;

    private BaseAdapter.OnItemChildClickListener<MediaItem> onItemClickListener;

    public VideoAlbumAdapter() {
        this.mList = new ArrayList<>();
    }

    @NonNull
    @Override
    public MediaFileViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        MediaFileViewHolder holder = new MediaFileViewHolder(new VideoAlbumItem(parent.getContext()));
        if (onItemClickListener != null) {
            holder.itemView.setOnClickListener(new HDOnClickListener() {
                @Override
                public void Click(View view) {
                    onItemClickListener.onItemChildClick(view,
                            mList.get(holder.getLayoutPosition()), holder.getLayoutPosition());
                }
            });
        }
        return holder;
    }


    @Override
    public void onBindViewHolder(@NonNull MediaFileViewHolder holder, int position) {
        MediaItem video = mList.get(position);
        boolean isSelected = isSelectedMediaItem(video);
        holder.getView(R.id.video_selected_iv).setSelected(isSelected);
        ImageView coverIv = holder.getView(R.id.video_cover_iv);
        coverIv.setColorFilter(coverIv.getResources().getColor(
                isSelected ? R.color.color_66000000 : R.color.transparent));
        if (video.getUri() != coverIv.getTag(R.id.video_cover_iv)) {
            coverIv.setTag(R.id.video_cover_iv, video.getUri());
            Glide.with(holder.itemView.getContext())
                    .asBitmap()
                    .load(video.getUri())
                    .apply(new RequestOptions()
                            .placeholder(R.drawable.img_video_album_default)
                            .diskCacheStrategy(DiskCacheStrategy.ALL)
                            .centerCrop()
                            .dontAnimate()
                            .transform(new CenterCrop(), new RoundedCorners(5))
                    )
                    .into((ImageView) holder.getView(R.id.video_cover_iv));
        }
        if (MimeType.isVideo(video.getMimeType())) {
            holder.getView(R.id.video_mark_iv).setVisibility(View.VISIBLE);
            holder.getView(R.id.video_duration_tv).setVisibility(View.VISIBLE);
            holder.setText(R.id.video_duration_tv, StringUtils.getFormatMillisTime(video.getDuration()));
        }else {
            holder.getView(R.id.video_duration_tv).setVisibility(View.GONE);
            holder.getView(R.id.video_mark_iv).setVisibility(View.GONE);
        }
    }

    public void setOnItemChildClickListener(BaseAdapter.OnItemChildClickListener<MediaItem> onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public void setSelectedMediaItemSet(MutableLiveData<LinkedHashMap<Long, MediaItem>> selectedVideoList) {
        this.mSelectedVideoList = selectedVideoList;
    }

    public void updateMediaItem(MediaItem mediaItem) {
        for (int i = 0; i < mList.size(); i++) {
            MediaItem item = mList.get(i);
            if (item == mediaItem) {
                notifyItemChanged(i);
                break;
            }
        }
    }

    private boolean isSelectedMediaItem(MediaItem video) {
        return mSelectedVideoList != null && Objects.requireNonNull(mSelectedVideoList.getValue()).containsKey(video.getId());
    }

    public void submitListOnSort(List<MediaItem> list) {
        if (mList == null) {
            mList = new ArrayList<>();
        }
        mList.clear();
        mList.addAll(list);
        notifyDataSetChanged();
    }


}
